package com.youhong.teethcare.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * App绠＄悊
 *
 * @author Jerry Lee
 */
public class AppManager {
    private static Stack<Activity> mActivityStack;
    private static AppManager mAppManager;

    private AppManager() {
    }

    /**
     * 鍗曚竴瀹炰緥
     */

    public int getTaskStackSize() {

        return mActivityStack == null ? 0 : mActivityStack.size();
    }

    public static AppManager getInstance() {
        if (mAppManager == null) {
            mAppManager = new AppManager();
        }
        return mAppManager;
    }

    /**
     * 娣诲姞Activity鍒板爢鏍�
     */
    public void addActivity(Activity activity) {
        if (mActivityStack == null) {
            mActivityStack = new Stack<Activity>();
        }
        mActivityStack.add(activity);
    }

    /**
     * 鑾峰彇鏍堥《Activity锛堝爢鏍堜腑鏈�鍚庝竴涓帇鍏ョ殑锛�
     */
    public Activity getTopActivity() {
        Activity activity = mActivityStack.lastElement();
        return activity;
    }

    public Activity getSpecificActivity(Class<?> c) {

        if (mActivityStack == null) {
            return null;
        }
        Activity result = null;

        for (Activity act : mActivityStack) {

            if (act.getClass() == c) {

                result = act;
            }
        }
        return result;
    }

    public boolean isSpecificActivityInStack(Class<?> c) {

        if (mActivityStack == null) {
            return false;
        }

        for (Activity act : mActivityStack) {

            if (act.getClass() == c) {

                return true;
            }
        }

        return false;
    }

    /**
     * 缁撴潫鏍堥《Activity锛堝爢鏍堜腑鏈�鍚庝竴涓帇鍏ョ殑锛�
     */
    public void killTopActivity() {

        if (mActivityStack == null) {
            return;
        }

        Activity activity = mActivityStack.lastElement();
        killActivity(activity);
    }

    /**
     * 缁撴潫鎸囧畾鐨凙ctivity
     */
    public void killActivity(Activity activity) {
        if (activity != null) {
            mActivityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 缁撴潫鎸囧畾绫诲悕鐨凙ctivity
     */
    public void killActivity(Class<?> cls) {

        if (mActivityStack == null) {

            return;
        }

        List<Activity> acts = new ArrayList<Activity>();

        for (Activity activity : mActivityStack) {
            if (activity.getClass().equals(cls)) {
                acts.add(activity);
            }
        }

        for (int i = 0; i < acts.size(); i++) {

            killActivity(acts.get(i));
        }
    }

    /**
     * 缁撴潫鎵�鏈堿ctivity
     */
    public void killAllActivity() {
        if (mActivityStack == null) {

            return;
        }

        for (int i = 0, size = mActivityStack.size(); i < size; i++) {
            if (null != mActivityStack.get(i)) {
                mActivityStack.get(i).finish();
            }
        }
        mActivityStack.clear();
    }

    /**
     * 閫�鍑哄簲鐢ㄧ▼搴�
     */
    public void AppExit(Context context) {
        try {
            killAllActivity();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
        } catch (Exception e) {
        } finally {
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

    public void PrintStack() {

        int index = 1;
        for (Activity act : mActivityStack) {

            Log.e("Index:" + index, act.getClass().getName());
            index++;
        }

    }

}
